/* 
 * 首页相关操作的js,需要依赖jQuery以及百度地图API
 */
//站点地址,由此地址生成各个页面的地址
var site_url = "./";
//显示模式， 0 - 普通微博展示， 1 - 公共墙微博展示
var DISPLAY_TYPE_NORMAL  = 0;
var DISPLAY_TYPE_SUGGEST = 1;
var display_type = DISPLAY_TYPE_NORMAL;
//全局地图
var map;
//保存各个位置的消息的HashMap，每个地点是一个Key，用经纬度标识
var mark_list = new HashMap();
//微博使用的token，暂时不用
var access_token;
//当前的InfoWindow，每次只允许打开一个InfoWindow
var currentInfoWindow = null;

/******************************
 *类定义
 */
//全局数组中保存的微博
function MixedWeibo(type) {
    this.type = type;       //0 - 普通微博， 1 - 公共墙微博；
    this.status = null;     //对应的微博，就是直接把新浪微博的一条微博的全部内容都存到这里了；
    this.id = -1;           //suggest表中的id，公共墙专用；
    this.weibo_id = -1;     //对应的微博的id，公共墙专用；
    this.weibo_mid = -1;    //对应的微博的mid，公共墙专用；
    this.suggest_user = -1; //推荐的用户的id，公共墙专用；
    this.like_num = 0;      //赞的个数，公共墙专用；
    this.to_group = 0;      //所属组，公共墙专用；
    this.add_time = "";     //添加时间，公共墙专用；
    this.is_like = "";      //是否已赞过，公共墙专用；
}

/******************************
 *函数定义
 */
//设置站点地址
function setUrl(url)
{
    site_url = url;
}

//设置access token，暂时不用
function setAccessToken(token)
{
    access_token = token;
}

//加载地图
function loadMap(container, type)
{
    display_type = type;
    
    if(typeof(google) == 'undefined') {
        alert("被墙啦...");
        return;
    }
    
    var p = new google.maps.LatLng(39.915, 116.404);
    var mapOpt = {
        center: p,
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(container, mapOpt);
    
    refresh();

    showCurrentCity();        //直接跳到当前城市
}

/**
 *显示当前城市
 **/
function showCurrentCity()
{
    
}

//刷新
function refresh()
{
    //显示提示框
    $("#loading_hint").show();
   
    var url = "";
    if(display_type == DISPLAY_TYPE_SUGGEST) {       //区分普通模式与公共墙模式
        url = site_url + "weibolist.php?action=suggest_get";
    } else {
        url = site_url + "weibolist.php?action=get_list";
    }
    
    //alert(url);
    $.post(
        url,
        null,
        function(data,status){
            $("#loading_hint").hide();
            if("success" == status)
            {
                //alert(data);
                on_refresh(data);
            }
        }
    );
}

//处理请求返回
function on_refresh(data)
{
    var json = eval("(" + data + ")");
    var ret = json.ret;
    
    if(ret == "success") {
        var loc_list = json.statuses;
        add_geoweibos(loc_list);
    } else {
        alert("Err code : " + json.err_code);
    }
}

//添加一组标注点
function add_geoweibos(arr)
{
    if(arr == null) {
        alert("arr == null , faild to add geoweibos");
        return;
    }
    
    for(var i = 0; i < arr.length; i++)
    {
        var latitude = 0.0;
        var longtitude = 0.0;
        var weibo = arr[i];
        var mixedweibo = new MixedWeibo(display_type);
        var valid = true;
    
        if(display_type == DISPLAY_TYPE_SUGGEST) {
            //公共墙模式，数据结构：
            /* “id”:1,
               “weibo_id”:123456,
               “weibo_mid”:123456,
               “latitude”:12.0,
               “longtitude”:12.1,
               “suggest_user”:123,
               “like_num”:15,
               “to_group”:0,
               “add_time”:”2013-08-10 17:41:55”
             */
            latitude = weibo.latitude;
            longtitude = weibo.longtitude;
            mixedweibo.id = weibo.id;
            mixedweibo.weibo_id = weibo.weibo_id;
            mixedweibo.weibo_mid = weibo.weibo_mid;
            mixedweibo.suggest_user = weibo.suggest_user;
            mixedweibo.like_num = parseInt(weibo.like_num);     //转换为数字
            mixedweibo.to_group = weibo.to_group;
            mixedweibo.add_time = weibo.add_time;
            mixedweibo.is_like = weibo.is_like;
        } else {
            //公共墙模式，数据结构：微博返回的标准结构
            var geo = weibo.geo;
            if(geo != null && geo.coordinates != null) {
                latitude = geo.coordinates[0];
                longtitude = geo.coordinates[1];
                mixedweibo.status = weibo;        //普通微博只设置status
                //alert(latitude + "," + longtitude);
            } else {
                valid = false;
            }
        }
        
        if (valid) {
            add_geoweibo(latitude, longtitude, mixedweibo);
        }
    }
}

//添加一条微博
function add_geoweibo(latitude, longtitude, mixedweibo)
{
    if(add_to_hashMap(latitude, longtitude, mixedweibo)) {
        var point = new google.maps.LatLng(latitude, longtitude);
        var marker = add_marker(point);

        var openInfoFunc = function(){
            onMarkerClicked(latitude, longtitude, marker);
        };

        google.maps.event.addListener(marker, 'click', openInfoFunc);
    }
    
    return;
}

//添加一个标注点
function add_marker(point)
{
    var marker = new google.maps.Marker({
        position: point,
        map: map
    });
    return marker;
}

/**
 *将地点信息添加到Mark HashMap里
 *@param latitude 纬度
 *@param longtitude 经度
 *@param mixedweibo 一条微博，类型：MixedWeibo
 *@return true - 新增marker; false - 已存在marker;
 */
function add_to_hashMap(latitude, longtitude, mixedweibo)
{
    var str = "(" + latitude + "," + longtitude + ")";
    var ret;
    var arr;
    if(mark_list.containsKey(str)) {
        arr = mark_list.get(str);
        arr.push(mixedweibo);
        ret = false;
    } else {
        arr = new Array();
        arr.push(mixedweibo);
        mark_list.put(str, arr);
        ret = true;
    }
    
    return ret;
}

//点击某个marker时调用的函数
function onMarkerClicked(latitude, longtitude, marker)
{
    var geo_str = "(" + latitude + "," + longtitude + ")";
    
    var arr = mark_list.get(geo_str);
    if(arr == null || arr.length <= 0) {
        alert("Error : Can't find geoweibo on this mark!");
        return;
    }
    
    //判断本地点的微博是否已经加载，如果未加载，则加载整个地点的所有微博，否则直接显示某个点的微博
    var first_weibo = arr[0];
    if(first_weibo != null && first_weibo.status == null) {
        load_Weibos(geo_str, arr, marker);
    } else {
        open_InfoWindowForMarker(geo_str, arr, marker);
    }
}

/**
 *加载一组微博
 *@param geo_str 地理坐标字符串形式(12.0, 12.1)
 *@param arr 要加载的微博列表，类型：MixedWeibo数组
 *@param marker 微博对应的marker
 **/
function load_Weibos(geo_str, arr, marker) {
    //显示加载框
    $("#loading_hint").show();
    
    //构造请求数组
    var list = new Array();
    var i = 0;
    for(i = 0; i < arr.length; i++) {
        var geoweibo = arr[i];
        //alert("push : " + geoweibo.weibo_id);
        list.push(geoweibo.weibo_mid);
    }
    //alert(list);
    
    //发出请求
    var url = site_url + "weibolist.php?action=weibo_get";
    $.post(
        url,
        {
            id_list : list
        },
        function(data,status){
            $("#loading_hint").hide();      //隐藏加载框
            if("success" == status)
            {
                //alert(data);
                on_load_Weibos(geo_str, data, arr, marker);
            }
        }
    );
}

/**
 *加载微博的回调函数
 *@param geo_str 地理坐标字符串形式(12.0, 12.1)
 *@param data 服务器返回的数据
 *@param arr 要加载的微博列表，类型：MixedWeibo数组
 *@param marker 微博对应的marker
 **/
function on_load_Weibos(geo_str, data, arr, marker) {
    var json = eval("(" + data + ")");
    var ret = json.ret;

    if("success" == ret)
    {
        var statuses = json.statuses;
        //补充statuses
        fill_weibo_status(arr, statuses);
        //打开infoWindow
        open_InfoWindowForMarker(geo_str, arr, marker);
    }
    else
    {
        alert("Error : " + data);
    }
}

/**
 *把新浪微博返回的微博数据添加到MixedWeibos数组对应微博的status中，根据id判断
 *@param mixedWeibos MixedWeibos数组
 *@param statuses 新浪微博返回的status数组
 */
function fill_weibo_status(mixedWeibos, statuses) {
    if(mixedWeibos == null || statuses == null) {
        alert("mixedWeibos == null || statuses == null!");
        return;
    }
    
    //TODO 性能优化
    var i = 0;
    var j = 0;
    for(i = 0; i < mixedWeibos.length; i++) {
        for(j = 0; j < statuses.length; j++) {
            var geoweibo = mixedWeibos[i];
            var status = statuses[i];
            if(geoweibo.weibo_mid == status.mid) {
                //alert("find same weibo_id : " + geoweibo.weibo_id);
                geoweibo.status = status;
                break;
            }
        }
    }
}

/**
 * 打开InfoWindow
 * @param geo_str 地理坐标字符串形式(12.0, 12.1)
 * @param arr 要加载的微博列表，类型：MixedWeibo数组
 * @param marker 微博对应的marker
 */
function open_InfoWindowForMarker(geo_str, arr, marker)
{
    if(arr == null || arr.length == 0) {
        alert("Invalid arr!");
        return;
    }
    
    //判断是一条微博还是多条
    var multiMsg = false;
    if(arr.length == 1) {
        multiMsg = false;
    } else {
        multiMsg = true;
    }
    
    //关闭之前的窗口
    if(currentInfoWindow != null) {
        currentInfoWindow.close();
    }
    
    var sContent;
    
    //根据不同模式显示不同内容
    if(multiMsg) {
        sContent = get_multiMsg_sContent(geo_str);
        
        currentInfoWindow = new google.maps.InfoWindow({
                        content: sContent
                     });
        
        currentInfoWindow.open(map, marker);
    } else {
        sContent = get_oneMsg_sContent(arr[0]);

        currentInfoWindow = new google.maps.InfoWindow({
                        content: sContent
                     });

        currentInfoWindow.open(map, marker);
        
        $("a#imgbox").fancybox({
            'transitionIn'    : 'none',
            'transitionOut'    : 'none'
        });
    }
}

//根据不同模式显示不同的操作按钮
function get_actionBar_content(mixedweibo) {
    var content = "";
    var geoweibo = mixedweibo.status;
    var latitude = 0.0;
    var longtitude = 0.0;
    if(geoweibo.geo != null) {
        latitude = geoweibo.geo.coordinates[0];
        longtitude = geoweibo.geo.coordinates[1];
    }
    
    if(display_type == DISPLAY_TYPE_SUGGEST) {
        //公共墙模式显示赞
        var like_num = mixedweibo.like_num;
        var img_src = "";
        if(mixedweibo.is_like == 1) {
            img_src = "./style/images/good_click.png";
        } else {
            img_src = "./style/images/good.png";
        }
        content = "<a id='info_good_btn' href='#' onClick='toggle_like(" + mixedweibo.id + "," + latitude + "," + longtitude +")'><img src='" + img_src + "' /> 赞(" + like_num + ")</a>";
    } else {
        //普通模式显示推荐按钮
        content = "<a id='info_suggest_btn' href='#' onClick='suggest_add("+ geoweibo.mid + "," + latitude + "," + longtitude +")'>喜欢</a>"
    }
    
    return content;
}

//得到某条微博的具体html内容
//参数: mixedweibo
function get_oneMsg_sContent(mixedweibo)
{
    var geoweibo = mixedweibo.status;
    
    //读取各个信息
    //var id = geoweibo.id;
    var mid = geoweibo.mid;
    var msg = geoweibo.text;
    var photo = geoweibo.original_pic;
    var comment_count = geoweibo.comments_count;
    var userid = geoweibo.user.id;
    var username = geoweibo.user.name;
    var uptime = geoweibo.created_at;
    var face_url = geoweibo.user.profile_image_url;
    var user_space_url = "http://weibo.com/" + userid + "/profile?rightmod=1&wvr=5&mod=personinfo";
    
    //附带图片的内容
    var photo_content = "";
    if(photo != null) {
        var photo_url = photo;
        photo_content = "<a id='imgbox' href='" + photo_url + "'><img id='imgDemo' src='" + photo_url + "' height='104'/></a>";
    }

    //更新时间
    //var uptime_t = getDateString(uptime);
    var uptime_t = uptime;

    //消息内容
    var sMessage =  "<div class='weibo_face'>" +
                        "<a target='_blank' href='" + user_space_url + "'>" +
                            "<img src='"+ face_url +"'>"+
                        "</a>"+
                    "</div>" +
                    "<div class='weibo_detail'>" +
                        "<div class='weibo_info'>" + "<a class='weibo_name' target='_blank' href='" + user_space_url + "'>" + username + "</a>" + "</div>" +
                        "<div class='weibo_text'>" + msg + "</div>" +
                        "<div class='weibo_pic' id='weibo_pic'>" + photo_content + "</div>" +
                        "<div class='weibo_func clearfix'>" +
                            "<div class='weibo_handle'>" +
                            get_actionBar_content(mixedweibo) +
                            "&nbsp&nbsp&nbsp|&nbsp&nbsp&nbsp" +
                            "<a id='info_comment_btn' href='#' onClick='toggle_comment("+ mid + "," + comment_count +")'>评论(" + comment_count + ")</a>" +
                            "</div>" +
                            "<div class='weibo_from'>" + uptime_t +"</div>" +
                        "</div>" +
                    "</div>";

    //评论的div内容
    var sComment = "<div class='weibo_comment_context'>" +
                        "<div class='weibo_comment' id='comment'>" +
                            "<div class='comment_input'>" +
                                "<textarea id='comment_text'></textarea>" +
                                "<input type='button' onclick='send_comment(" + mid +")' value='提交'/>" +
                            "</div>" +

                            "<div class='comment_list'>" +
                                "<p id='comment_hint'></p>" +
                                "<table id='comment_table'>" +
                                "</table>" +
                            "</div>" +
                        "</div>" +
                    "</div>";

    var sContent = "<div class='weibo_feed_detail infoWindow_content'>" + sMessage + sComment + "</div>";
    
    return sContent;
}

//得到某个地点一组微博的具体html内容
function get_multiMsg_sContent(str)
{
    var arr = mark_list.get(str);
    if(arr == null || arr.length == 0) {
        alert("Error : Can't find geoweibo on this mark : " + str);
        return "";
    }
    
    var sMessageList = "<table>";
    for(var i = 0; i < arr.length; i++) {
        var geoweibo = arr[i].status;
        var mid = geoweibo.mid;
        var face_url = geoweibo.user.profile_image_url;
        var username = geoweibo.user.name;
        var photo = geoweibo.original_pic;
        var msg = geoweibo.text;
        var uptime = geoweibo.created_at;

        //var uptime_t = getDateString(uptime);
        var uptime_t = uptime;
        
        var photo_content = "";
        //显示是否带有图片的小图标
        if(photo != null) {
            photo_content = "<img src='" + site_url + "/style/images/pic.png" + "' height='15'/>";
        }

        sMessageList += "<tr onclick='open_detailMsgBox(" + mid + ',"' + str + '"' + ")'>" + 
                        "<td class='multi_msg_face_td'>" + "<img src='" + face_url + "'/>" + "</td>" +
                        "<td>" + username + ": " + msg + 
                            "</br>" +
                            photo_content + " (" + uptime_t + ")" + 
                        "</td>" +
                    "</tr>";
    }
    sMessageList += "</table>";
    
    var sContent = "<div class='multi_msg_list infoWindow_content'>" + sMessageList + "</div>";
    
    return sContent;
}

//打开某条详细信息
function open_detailMsgBox(mid, marker)
{
    var arr = mark_list.get(marker);
    if(arr == null || arr.length == 0) {
        alert("Error : Can't find geoweibo on this mark!");
        return;
    }
    
    var mixedweibo = null;
    var i = 0;
    for(i = 0; i < arr.length; i++) {
        if(arr[i].status.mid == mid) {
            mixedweibo = arr[i];
            break;
        }
    }
    if(mixedweibo == null) {
        alert("Error : Can't find geoweibo for mid :" + mid);
        return;
    }
    
    //构造header内容
    var content = "<div class='weibo_header'><a onclick='open_MsgList(" + '"' + marker + '"' + ")'>返回列表</a>  ";
    //第一条以外添加上一条 
    if(i > 0) {
        content = content + "<a onclick='open_detailMsgBox(" + arr[i - 1].status.mid + ',"' + marker + '"' + ")'><img class='pointer_img' src='" + site_url + "/style/images/pointer_left.png" + "' /></a>"
    } else {
        content = content + "<img class='pointer_img_invalid' src='" + site_url + "/style/images/pointer_left_invalid.png" + "' />";
    }
    //加点间隔
    content = content + "    ";
    //最后一条以外添加下一条
    if(i < (arr.length - 1)) {
        content = content + "<a onclick='open_detailMsgBox(" + arr[i + 1].status.mid + ',"' + marker + '"' + ")'><img class='pointer_img' src='" + site_url + "/style/images/pointer_right.png" + "' /></a>"
    } else {
        content = content + "<img class='pointer_img_invalid' src='" + site_url + "/style/images/pointer_right_invalid.png" + "' />";
    }
    content = content + "</div>";
    //构造主题内容
    content = content + get_oneMsg_sContent(mixedweibo);
    
    currentInfoWindow.setContent(content);
    
    $("a#imgbox").fancybox({
            'transitionIn'    : 'none',
            'transitionOut'    : 'none'
    });
        
    return;
}

function open_MsgList(marker)
{
    var content = get_multiMsg_sContent(marker);
    currentInfoWindow.setContent(content);
    
    //currentInfoWindow.redraw();
    
    return;
}

//显示/隐藏评论框
function toggle_comment(mid, comment_count)
{
    var cnt = $("#comment_table tr").size();
    
    if(cnt > 0 || comment_count == 0 || $("#comment_hint").html() != "") {
        //如果已经有评论或者没有评论，或者正在读取评论，就什么都不做
    } else {
        //否则读取评论
        //alert("get comment now...");
        get_comment(mid);
    }
    
    /*
    if(cmt.is(":visible")) {        //隐藏评论框
        cmt.hide();
    } else {        //显示评论框
        cmt.show();
        get_comment(mid);
    }*/
    
    //currentInfoWindow.redraw();
}

//读取评论
function get_comment(mid)
{
    //显示提示
    $("#comment_hint").html("正在读取评论...");
    
    //开始读取
    var url = site_url + "weibolist.php?action=get_comment";
    $.post(
       url,
       {
           geoweibo_mid : mid
       },
       function(data,status){
           $("#comment_hint").html("");     //隐藏提示
           if("success" == status)
           {
               //alert(data);
               on_get_comment(data);
           }
       }
    );
}

//检查评论返回
function on_get_comment(data)
{
    var json = eval("(" + data + ")");
    var ret = json.ret;

    if("success" == ret)
    {
        var comment_list = json.comments;
        add_comment_to_list(comment_list);
    }
    else
    {
        alert("Error : " + data);
    }
}

//将评论放到界面上
function add_comment_to_list(arr)
{
    var list = $("#comment_table");
    list.empty();
    
    //修改评论数
    var comments_count = arr.length;
    var comment_btn = $("#info_comment_btn");
    comment_btn.html("评论(" + comments_count + ")");
    //修改全局数组里存储的评论数
    if(arr.length >= 1) {
        var geo = arr[0].status.geo;
        var id = arr[0].status.mid;
        if(geo != null && geo.coordinates != null)
        {
            var latitude = geo.coordinates[0];
            var longtitude = geo.coordinates[1];
            mark_list_modify_comment_cnt(latitude, longtitude, id, comments_count);
        }
    }
    
    for(i = 0; i < arr.length; i++)
    {
        add_one_comment_to_list(arr[i], list);
    }
}

//修改全局数组中某条微博的评论数
function mark_list_modify_comment_cnt(latitude, longtitude, id, comments_count) {
    //alert(latitude + "," + longtitude + "," + id + "," + comments_count);
    var str = "(" + latitude + "," + longtitude + ")";
    
    var arr = mark_list.get(str);
    if(arr == null || arr.length == 0) {
        alert("mark_list_modify_comment_cnt : Can't find geoweibo on this mark!");
        return;
    }
    
    var i = 0;
    var geoweibo = null;
    for(i = 0; i < arr.length; i++) {
        if(arr[i].status.mid == id) {
            geoweibo = arr[i].status;
            //alert("Found geoweibo, current count : " + geoweibo.comments_count);
            break;
        }
    }
    
    if(geoweibo != null) {
        geoweibo.comments_count = comments_count;
    }
}

//添加一条评论到列表
function add_one_comment_to_list(cmt, list) {
    if(cmt == null || list == null) {
        return;
    }
    
    var userid = cmt.user.id;
    var username = cmt.user.name;
    var comment = cmt.text;
    var addtime = cmt.created_at;
    //var display_time = getDateString(addtime);
    var display_time = addtime;
    var face_url = cmt.user.profile_image_url;

    var user_space_url = "http://weibo.com/" + userid + "/profile?rightmod=1&wvr=5&mod=personinfo";

    var str = "<tr>" + 
                "<td class='comment_face_td'>" + "<a target='_blank' href='" + user_space_url + "'>" + "<img src='" + face_url + "'/>" + "</a>" +"</td>" +
                "<td>" + 
                    "<a target='_blank' href='" + user_space_url + "'>" + username + "</a>" + ": " + comment + 
                    "<br />" +
                    " (" + display_time + ")" + 
                "</td>" +
              "</tr>";
    list.append(str);
    
    return;
}

//发出一条评论
function send_comment(mid)
{
    var url = site_url + "weibolist.php?action=send_comment";
    var text = $("#comment_text").val();
    
    //alert(id + ", " + text);
    $.post(
        url,
        {
            geoweibo_mid : mid,
            comment_text: text
        },
        function(data,status){
            if("success" == status)
            {
                on_comment_sended(mid, data);
            }
        }
    );
}

//成功评论后调用的函数
function on_comment_sended(mid, data) {
    var json = eval("(" + data + ")");
    var ret = json.ret;
    
    if(ret == "success") {
        get_comment(mid);
    } else {
        alert("Err code : " + json.err_code);
    }
}

//推荐一条微博
function suggest_add(mid, latitude, longtitude)
{
    var mixedWeibo = get_MixedWeiboByIdByWeiboMid(latitude, longtitude, mid);
    
    //读取各个基本信息
    var id = "";
    var weibo_text = "";
    var weibo_nickname = "";
    if(mixedWeibo != null) {
        var geoweibo = mixedWeibo.status;
        if(geoweibo != null) {
            id = geoweibo.id;
            weibo_text = geoweibo.text;
            if(geoweibo.user != null) {
                weibo_nickname = geoweibo.user.screen_name;
            }
        }
    }
    
    var url = site_url + "weibolist.php?action=suggest_add";
    
    alert(id + ", " + latitude + ", " + longtitude + "," + weibo_text + "," + weibo_nickname);
    $.post(
        url,
        {
            geoweibo_id : id,
            geoweibo_mid : mid,
            latitude: latitude,
            longtitude: longtitude,
            weibo_text: weibo_text,
            weibo_nickname: weibo_nickname
        },
        function(data,status){
            if("success" == status)
            {
                //alert(data);
                on_suggest_added(data);
            }
        }
    );
}

//微博推荐成功后调用的函数
function on_suggest_added(data) {
    var json = eval("(" + data + ")");
    var ret = json.ret;
    
    if(ret == "success") {
        alert("添加成功");
    } else {
        var err = json.err_code;
        if(err == "err_exist") {
            alert("已添加过");
        } else {
            alert("Err code : " + json.err_code);
        }
    }
}

//赞/取消赞一条微博
function toggle_like(weibo_id, latitude, longtitude)
{
    var url = site_url + "weibolist.php?action=like_act";
    var act = "";
 
    var mixedweibo = get_MixedWeiboById(latitude, longtitude, weibo_id);
    if(mixedweibo == null) {
        alert("toggle_like, mixedweibo == null!");
        return;
    }
    
    var like;
    if(mixedweibo.is_like == 1) {
        //当前已经赞，取消赞
        act = "del";
        like = false;
    } else {
        //当前没有赞，赞一个
        act = "add";
        like = true;
    }
    
    //alert(weibo_id + ", " + act);
    $.post(
        url,
        {
            suggest_weibo_id : weibo_id,
            act : act
        },
        function(data,status){
            if("success" == status)
            {
                //alert(data);
                adjust_like_data_display(mixedweibo, like);
                on_toggle_like(data);
            }
        }
    );
}

/**
 *调整界面上赞的相关展示，设置数据记录
 *@param mixedweibo 要设置的mixedweibo
 *@param like boolean型，设置为赞或者取消赞
 */
function adjust_like_data_display(mixedweibo, like) {
    if(mixedweibo == null) {
        alert("Invalid mixedweibo!");
        return;
    }
    
    var img_src;
    if(like) {
        //添加赞
        img_src = "./style/images/good_click.png";
        mixedweibo.is_like = 1;
        mixedweibo.like_num = mixedweibo.like_num + 1;
    } else {
        //取消赞
        img_src = "./style/images/good.png";
        mixedweibo.is_like = 0;
        mixedweibo.like_num = mixedweibo.like_num - 1;
        if(mixedweibo.like_num < 0) {
            mixedweibo.like_num = 0;
        }
    }
    
    //设置显示
    //<img src='" + img_src + "' /> 赞(" + like_num + ")
    $("#info_good_btn").html("");
    $("#info_good_btn").html("<img src='" + img_src + "' /> 赞(" + mixedweibo.like_num + ")");
    
    return;
}

//赞操作完成后调用的函数
function on_toggle_like(data) {
    var json = eval("(" + data + ")");
    var ret = json.ret;
    
    if(ret == "success") {
        //alert("操作成功");
    } else {
        var err = json.err_code;
        if(err == "err_exist") {
            alert("已赞过");
        } else if(err == "err_not_exist") {
            alert("还没赞过");
        } else {
            alert("Err code : " + json.err_code);
        }
    }
}

//读取某个经纬度上某个id对应的mixedweibo数组
function get_MixedWeibos(latitude, longtitude) {
    var geo_str = "(" + latitude + "," + longtitude + ")";
    
    var arr = mark_list.get(geo_str);
    
    return arr;
}

//读取某个经纬度上某个id对应的mixedweibo，用于推荐模式
function get_MixedWeiboById(latitude, longtitude, id) {
    var geo_str = "(" + latitude + "," + longtitude + ")";
    
    var arr = mark_list.get(geo_str);
    if(arr == null || arr.length <= 0) {
        alert("Error : Can't find geoweibo on this mark!");
        return null;
    }
    
    var ret = null;
    for(var i = 0; i < arr.length; i++) {
        if(arr[i].id == id) {
            //alert("find id : " + id);
            ret = arr[i];
            break;
        }
    }
    
    return ret;
}

//根据微博mid从全局数组中查找到对应的mixedWeibo对象
function get_MixedWeiboByIdByWeiboMid(latitude, longtitude, mid) {
    var geo_str = "(" + latitude + "," + longtitude + ")";
    
    var arr = mark_list.get(geo_str);
    if(arr == null || arr.length <= 0) {
        alert("Error : Can't find geoweibo on this mark!");
        return null;
    }
    
    var ret = null;
    for(var i = 0; i < arr.length; i++) {
        if(arr[i].status != null && arr[i].status.mid == mid) {
            alert("find mid : " + mid);
            ret = arr[i];
            break;
        }
    }
    
    return ret;
}